<?php 
include("connection/connection.php");
if(isset($_POST['delete']))
{
	$hp_id=$_POST['hp_id'];
	$loc_id=$_POST['loc_id'];
	$habit_code = $_POST['habit_code'];
	
	mysql_query("DELETE FROM tbl_dtls_q2_hp_status WHERE id='$hp_id' AND q2_id='$loc_id' ");
	/*
	//Code for update HP and SP nos wher delete operation happend
	if($_REQUEST['code']=="HP")
	{ 
		mysql_query("UPDATE tbl_dtls_q2_habit SET hp_no = (hp_no - 1) WHERE q2_id='$loc_id' AND habit_code='$habit_code'");
	}
	if($_REQUEST['code']=="SP")
	{
		mysql_query("UPDATE tbl_dtls_q2_habit SET sp_no=(sp_no - 1) WHERE q2_id='$loc_id' AND habit_code='$habit_code'");
	}
	*/
	
	//function for count total no. of HP and SP and get value to calling function	
	function countSource($hcode,$hp_sp)
	{
		$qryCount="select count(*) as no from tbl_dtls_q2_hp_status where habit_code='$hcode' and hp_sp='$hp_sp'";
		$recCount=mysql_query($qryCount);
		$rowCount=mysql_fetch_array($recCount);
		return $rowCount[0];
	}
	//Update thevalue of HP and SP in HABITATION DETAIL PAGE
	$qryHabit="select * from tbl_dtls_q2_habit where q2_id='$loc_id' group by habit_code";
	$recHabit=mysql_query($qryHabit);
	while($rowHabit=mysql_fetch_array($recHabit))
	{
		$totalHP=countSource("$rowHabit[habit_code]","HP");
		$totalSP=countSource("$rowHabit[habit_code]","SP");
	
		$a="update tbl_dtls_q2_habit set hp_no='$totalHP',sp_no='$totalSP' where habit_code='$rowHabit[habit_code]'";
		mysql_query($a);
	}
//Code for maintain User history on delete operation
$qryHistory="insert into tbl_new_user_history(user_id,form_id,action_id,ip_address,modify_date,user_type,user_name) values('$_POST[loginId]','$loc_id','110','$_SERVER[REMOTE_ADDR]',NOW(),'$_POST[userType]','$_POST[name]')";
mysql_query($qryHistory);


	header("location:q2.php");

}		
?>